﻿using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace nMongo
{
    public static class MongoCollectionExtensions
    {
        public static MongoCollectionIdentity GetObjectIdentity<T>(this MongoCollection<T> collection, T oneObject)
        {
            MongoCollectionIdentity returned = new MongoCollectionIdentity();
            PropertyInfo property = typeof(T)
                .GetProperties()
                .FirstOrDefault(
                    x => x.CustomAttributes.Any(
                        y => y.AttributeType == typeof(BsonIdAttribute)));
            if (property != null)
            {
                returned.Value = property.GetValue(oneObject);
                returned.Type = property.PropertyType;
            }
            return returned;
        }
    }
}
